package com.somnus.androidkotlindemo.activity

import android.content.Intent
import android.os.Bundle
import android.os.PersistableBundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.somnus.androidkotlindemo.R
import com.somnus.androidkotlindemo.adapter.BaseRecyclerTestAdapter
import com.somnus.androidkotlindemo.adapter.BaseRecyclerTopAdapter
import com.somnus.androidkotlindemo.bean.StaffBean
import com.somnus.androidkotlindemo.kt.toast
import kotlinx.android.synthetic.main.activity_base_recycler_adapter.*

/**
 * BaseRecyclerViewHepler 框架  https://github.com/CymChad/BaseRecyclerViewAdapterHelper/
 * 仿iOS 侧滑删除 @see  : swipemenuview
 */
class BaseRecyclerAdapterActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_base_recycler_adapter)


        val topAdapter = BaseRecyclerTopAdapter();
        val gridManager = GridLayoutManager(this, 4)
        recycler_top.layoutManager = gridManager
        recycler_top.adapter = topAdapter

        topAdapter.setNewData(initTopData())

        val mHearView : View = LayoutInflater.from(this).inflate(R.layout.item_top_recycler,null)
        topAdapter.addHeaderView(mHearView)
//        topAdapter.setSpanSizeLookup { gridLayoutManager, position ->
//            return@setSpanSizeLookup if (position == 0) 4 else 1
//        }


        val adapter = BaseRecyclerTestAdapter()

        recycler.layoutManager = LinearLayoutManager(this)
        recycler.adapter = adapter
        adapter.setOnLoadMoreListener(BaseQuickAdapter.RequestLoadMoreListener {
            Log.d("tag", "s")
            adapter.loadMoreEnd(false)
        }, recycler)

        adapter.setNewData(initData())


//        adapter.setOnItemClickListener {adapter, view, position ->
//            val mUser : User = adapter.getItem(position) as User
//            toast(mUser.name!!)
//        }

        adapter.setOnItemChildClickListener { adapter, view, position ->
            val mUser: StaffBean = adapter.getItem(position) as StaffBean
            when (view.id) {
                R.id.content_view -> toast(mUser.staffName!!)
                R.id.btn_delete -> {
                    adapter.remove(position)
                }
            }
        }

    }

    override fun getIntent(): Intent {
        return super.getIntent()
    }

    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
    }

    override fun getPackageCodePath(): String {
        return super.getPackageCodePath()
    }

    private fun initTopData(): List<StaffBean> {
        val list = ArrayList<StaffBean>()
        for (i in 0..5) {
            val user = StaffBean()
            user.staffAge = 18
            user.staffName = "测试" + i
            list.add(i, user)
        }
        return list
    }

    private fun initData(): List<StaffBean> {
        var list = ArrayList<StaffBean>()
        //0-49
        for (i in 0..50) {
            var user = StaffBean()
            user.staffAge = 18
            user.staffName = "测试" + i
            list.add(i, user)
        }
        return list
    }
}
